package cn.exrick.xboot.modules.content.serviceimpl.mybatis;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.content.dao.HontDepartmentDao;
import cn.exrick.xboot.modules.content.dao.mapper.HontDepartmentPostMapper;
import cn.exrick.xboot.modules.content.entity.HontDepartment;
import cn.exrick.xboot.modules.content.entity.HontDepartmentPost;
import cn.exrick.xboot.modules.content.entity.HontVo.HontDepartmentPostPo;
import cn.exrick.xboot.modules.content.entity.HontVo.HontDepartmentPostTree;
import cn.exrick.xboot.modules.content.entity.HontVo.HontDepartmentPostVo;
import cn.exrick.xboot.modules.content.service.HontDepartmentService;
import cn.exrick.xboot.modules.content.service.mybatis.IHontDepartmentPostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-10-19
 */
@Service
@Transactional
@Slf4j
public class HontDepartmentPostServiceImpl extends ServiceImpl<HontDepartmentPostMapper, HontDepartmentPost> implements IHontDepartmentPostService {
    @Autowired
    private HontDepartmentPostMapper hontDepartPostMapper;
    @Autowired
    private HontDepartmentDao departmentDao;
    @Override
    public void addBatchHontDepartmentPost(List<HontDepartmentPost> post) {
        hontDepartPostMapper.insertBatch(post);
    }

    @Override
    public void updateBatchHontDepartmentPost(List<HontDepartmentPost> post,String postId) {
        QueryWrapper<HontDepartmentPost> query = Wrappers.query();
        query.lambda().eq(HontDepartmentPost::getHontPostId,postId);
        hontDepartPostMapper.delete(query);
        addBatchHontDepartmentPost(post);
    }

    @Override
    public  List<HontDepartmentPostTree> getHontPostByDepartId(String departId) {
        List<HontDepartmentPostTree> data =new ArrayList<>();
        HontDepartmentPostPo hontDepartmentPostPo=new HontDepartmentPostPo();
        hontDepartmentPostPo.setHontDepartmentId(departId);
        //获取部门岗位
        List<HontDepartmentPostVo> hotList=    hontDepartPostMapper.getHontPostByDepartId(hontDepartmentPostPo);
        //获取所有部门
        List<HontDepartment> listAll=departmentDao.findAll();
        if(!StringUtils.isBlank(departId)){
            //获取当前部门并组装子集数据
            HontDepartment hontDepartment=listAll.stream().filter(t->departId.equals(t.getId())).collect(Collectors.toList()).get(0);
            List<HontDepartmentPostTree> result= departTree(departId,listAll,hotList);
            List<HontDepartmentPostTree> hontDepartmentPostVo=departTreeHontDepartmentPostVo(departId,hotList);
            if(hontDepartmentPostVo!=null&&hontDepartmentPostVo.size()>0){

                result.addAll(hontDepartmentPostVo);
            }
            HontDepartmentPostTree hontDepartmentPost=new HontDepartmentPostTree();
            hontDepartmentPost.setValue(hontDepartment.getId());
            hontDepartmentPost.setLabel(hontDepartment.getTitle());
            hontDepartmentPost.setChildren(result);
            data.add(hontDepartmentPost);
        }else{
            data=departTree("0",listAll,hotList);
        }

        return data;
    }

    /**
     * 匹配岗位
     * @param departId
     * @param hontDepartmentPostVoList
     * @return
     */
    public List<HontDepartmentPostTree> departTreeHontDepartmentPostVo(String departId, List<HontDepartmentPostVo> hontDepartmentPostVoList){
        List<HontDepartmentPostTree> result=new ArrayList<>();
        for (HontDepartmentPostVo hontDepartmentPostVo:hontDepartmentPostVoList){
          String departmentIds=  hontDepartmentPostVo.getDepartmentIds();
          if(StringUtils.isNotBlank(departmentIds)){
              List<String> ids= Arrays.asList(departmentIds.split(","));
              List<String>  id=ids.stream().filter(t->departId.equals(t)).collect(Collectors.toList());
              if(id.size()>0){
                  HontDepartmentPostTree hontDepartmentPost=new HontDepartmentPostTree();
                  hontDepartmentPost.setValue(hontDepartmentPostVo.getId());
                  hontDepartmentPost.setLabel(hontDepartmentPostVo.getTitle());
                  result.add(hontDepartmentPost);
              }
          }
        }
        return result;
    }

    /**
     * 组装部门树
     * @param parentId
     * @param listAll
     * @param hotList
     * @return
     */
    public List<HontDepartmentPostTree> departTree(String parentId, List<HontDepartment> listAll, List<HontDepartmentPostVo> hotList){
        List<HontDepartment> list=listAll.stream().filter(t->parentId.equals(t.getParentId())).collect(Collectors.toList());
        List<HontDepartmentPostTree> results=new ArrayList<>();
        for(HontDepartment itme:list){
            HontDepartmentPostTree hontDepartmentPostTree=new HontDepartmentPostTree();
            hontDepartmentPostTree.setValue(itme.getId());
            hontDepartmentPostTree.setLabel(itme.getTitle());
            List<HontDepartmentPostTree> children= departTree(itme.getId(),listAll,hotList);
            List<HontDepartmentPostTree> hontDepartmentPostVo=departTreeHontDepartmentPostVo(itme.getId(),hotList);
            if(hontDepartmentPostVo!=null&&hontDepartmentPostVo.size()>0){

                children.addAll(hontDepartmentPostVo);
            }
            hontDepartmentPostTree.setChildren(children);
            results.add(hontDepartmentPostTree);
        }
        return results;
    }
    @Override
    public List<HontDepartmentPostVo> getHontPostByPosts(List<String> postIds) {
        HontDepartmentPostPo hontDepartmentPostPo=new HontDepartmentPostPo();
        hontDepartmentPostPo.setHontPostIds(postIds);
        return hontDepartPostMapper.getHontPostByDepartId(hontDepartmentPostPo);
    }

    @Override
    public List<HontDepartmentPost> getHontPostByPostId(String postId) {
        QueryWrapper<HontDepartmentPost> query = Wrappers.query();
        query.lambda().eq(HontDepartmentPost::getHontPostId,postId);
        List<HontDepartmentPost> list=hontDepartPostMapper.selectList(query);
        return list;
    }
}
